let questionApp = new Vue({
    el: "#questionApp",
    data: {
        question: {}
    },
    methods: {
        loadQuestion: function () {
            let qid = location.search;
            if (!qid) {
                alert("请指定问题的id!")
                return;
            }
            qid = qid.substring(1);
            axios({
                url: "/v1/questions/" + qid,
                method: "get"
            }).then(function (response) {
                questionApp.question = response.data;
                addDuration(response.data);
            })
        }
    },
    created: function () {
        this.loadQuestion();
    }
})


let postAnswerApp = new Vue({
    el: "#postAnswerApp",
    data: {},
    methods: {
        postAnswer: function () {
            let qid = location.search;
            if (!qid) {
                return;
            }
            qid = qid.substring(1);
            let content = $("#summernote").val();
            let form = new FormData;
            form.append("questionId", qid);
            form.append("content", content);
            axios({
                url: "/v1/answers",
                method: "post",
                data: form
            }).then(function (response) {
                console.log(response.data);
            })
        }
    }
})


let answersApp = new Vue({
    el: "#answersApp",
    data: {
        answers: []
    },
    methods: {
        loadAnswers: function () {
            let qid = location.search;
            if (!qid) {
                return;
            }
            qid = qid.substring(1);
            axios({
                url: "/v1/answers/question/" + qid,
                method: "get"
            }).then(function (response) {
                answersApp.answers = response.data;
                answersApp.updateDuration();
            })
        },
        updateDuration:function () {
            let answers=this.answers;
            for (let i=0;i<answers.length;i++){
                addDuration(answers[i]);
            }
        },
        postComment:function (answersId) {
            let textarea =$("#addComment"+answersId+" textarea");
            let content=textarea.val();

            let form =new FormData();
            form.append("answerId",answersId);
            form.append("content",content);
            axios({
                url:"/v1/comments",
                method:"post",
                data:form
            }).then(function (response) {

            })
        }
    },
    created: function () {
        this.loadAnswers();
    }

})